<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        .judgeWrapper {
            width: 600px;
            margin: 50px auto;
        }

        .judgeBox {
            height: 264px;
            position: relative;
        }

        .judgeBox img {
            width: 100%;
        }

        .judgeBox .boxA,
        .judgeBox .boxB {
            width: 50px;
            height: 50px;
            background-color: rgba(255, 255, 255, 0.8);
            position: absolute;
        }

        .judgeBox .boxA {
            left: 0;
            top: 100px;
            background: url(../images/bg.jpg) no-repeat -300px -100px/600px auto;
            z-index: 1;
        }

        .judgeBox .boxB {
            left: 300px;
            top: 100px;
        }

        .slideBar {
            width: 100%;
            height: 50px;
            /* border: 1px solid #333; */
            margin-top: 10px;
            border-radius: 25px;
            position: relative;
            background-color: grey;
        }

        .slideBar .ball {
            width: 50px;
            height: 50px;
            background-color: pink;
            position: absolute;
            left: 0;
            border-radius: 50%;
        }
    </style>
</head>

<body>
    <div class="judgeWrapper">
        <div class="judgeBox">
            <img src="../images/bg.jpg" alt="">
            <div class="boxA"></div>
            <div class="boxB"></div>
        </div>
        <div class="slideBar">
            <div class="ball"></div>
        </div>
    </div>
</body>
<script>

    var judgeWrapper = document.getElementsByClassName("judgeWrapper")[0];
    var judgeBox = document.getElementsByClassName("judgeBox")[0];
    var slideBar = document.getElementsByClassName("slideBar")[0];
    var ball = document.getElementsByClassName("ball")[0];

    var boxA = document.getElementsByClassName("boxA")[0];
    var boxB = document.getElementsByClassName("boxB")[0];


    // 问题: maxTop => 计算有误?  => 经分析judgeBox有问题
    // 解析: 图片加载时需要时间的, 如果页面加载完毕,图片还未加载完 => 就无法撑开存放大图片父盒子judgeBox(此时高度21), 当然如果页面加载完毕,图片也已经加载=>没问题

    // 怎么解决?
    // 1. 给包裹图片的盒子设置定高  => 图片的高度就定死了
    // 2. 将所有js代码放到window.onload中 => 等所有内容加载完毕之后在执行js代码



    var maxLeft = slideBar.clientWidth - ball.clientWidth;
    var maxTop = judgeBox.clientHeight - boxB.clientHeight;
    console.log(judgeBox.clientHeight, boxB.clientHeight);

    // 页面加载时随机位置
    var randX = 200 + Math.round(Math.random() * (maxLeft - 200));
    var randY = Math.round(Math.random() * maxTop);

    boxB.style.left = randX + "px";
    boxB.style.top = randY + "px";
    boxA.style.top = randY + "px";
    // boxA.style.backgroundPosition = "-300px -100px";
    // boxA.style.backgroundPosition = -randX + "px " + (-randY) + "px";
    // boxA.style.backgroundPositionX = -randX + "px";
    // boxA.style.backgroundPositionY = -randY + "px";
    boxA.style.backgroundPosition = `-${randX}px -${randY}px`;

    ball.onmousedown = function (e) {
        var e = e || window.event;
        var posX = e.offsetX;
        var posY = e.offsetY;

        document.onmousemove = function (e) {
            var e = e || window.event;
            var x = e.pageX - posX - slideBar.offsetLeft;

            if (x < 0) x = 0;
            if (x > maxLeft) x = maxLeft;

            ball.style.left = x + "px";
            boxA.style.left = x + "px";



        }
        document.onmouseup = function (e) {
            document.onmousemove = null;

            // 当鼠标松开时 A和B重叠 => 验证成功?
            // 理想情况下:  boxA.offsetLeft == boxB.offsetLeft  => 对手残党不友好

            // 解决方法: 设置误差允许范围 => 2px
            // boxA.offsetLeft >= boxB.offsetLeft - 2  && boxB.offsetLeft <= boxB.offsetLeft + 2
            // Math.abs(boxA.offsetLeft-boxB.offsetLeft) <= 2


            if (boxA.offsetLeft > 0) {  //boxA移动
                if (Math.abs(boxA.offsetLeft - boxB.offsetLeft) <= 2) {
                    console.log("验证成功");
                } else {
                    console.log("验证失败");
                    ball.style.left = 0 + "px";
                    boxA.style.left = 0 + "px";

                    // 验证失败 再次随机位置
                    var randX = 200 + Math.round(Math.random() * (maxLeft - 200));
                    var randY = Math.round(Math.random() * maxTop);

                    boxB.style.left = randX + "px";
                    boxB.style.top = randY + "px";
                    boxA.style.top = randY + "px";
                    boxA.style.backgroundPosition = `-${randX}px -${randY}px`;
                }
            }





        }

    }

</script>

</html>